package com.avic.rong.goodsSort.activity.secondListActivity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.goodsSort.CarView;
import com.avic.rong.goodsSort.activity.GoodsSortActivity;
import com.avic.rong.goodsSort.activity.MaxRecyclerView;
import com.avic.rong.goodsSort.adapter.drawerAdapter.GoodsSortWdDrawerRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortJieJueShowRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortWdShowRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortWdShowRvSxAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortWdShowTimeRvAdapter;
import com.avic.rong.goodsSort.api.drawerApi.GoodsSortWdDrawerApi;
import com.avic.rong.goodsSort.api.secondListApi.GoodsSortSearchAllWdApi;
import com.avic.rong.goodsSort.api.secondListApi.GoodsSortSearchAllWdSxApi;
import com.avic.rong.goodsSort.api.secondListApi.GoodsSortSearchAllWdTimeApi;
import com.avic.rong.goodsSort.bean.secondDrawerBean.GoodsSortWdDrwerBean;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllWdBean;
import com.avic.rong.shoppingCar.activity.ShoppingCarActivity;
import com.avic.rong.shoppingCar.api.ShoppingCarGoodsKuCunApi;
import com.avic.rong.shoppingCar.api.ShoppingCarListApi;
import com.avic.rong.shoppingCar.bean.ShoppingCarListBean;
import com.avic.rong.userOrder.api.AddCarApi;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.DeviceUtils;
import com.avic.rong.utils.EditUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoodsSortWdActivity extends BaseActivity implements View.OnClickListener, GoodsSortWdDrawerRvAdapter.GoodsSortWdDrawerListener, GoodsSortWdShowRvAdapter.GoodsWdListener, GoodsSortWdShowRvSxAdapter.GoodsWdSxListener,GoodsSortWdShowTimeRvAdapter.GoodsWdTimeListener {

    @BindView(R.id.goods_sort_wd_back)
    ImageView goodsSortWdBack;
    @BindView(R.id.goods_sort_wd_fenlei)
    ImageView goodsSortWdFenlei;
    @BindView(R.id.goods_sort_bz_edit)
    EditText goodsSortBzEdit;
    @BindView(R.id.goods_sort_wd_title)
    RelativeLayout goodsSortWdTitle;
    @BindView(R.id.goods_sort_wd_zh)
    RadioButton goodsSortWdZh;
    @BindView(R.id.goods_sort_wd_time)
    RadioButton goodsSortWdTime;
    @BindView(R.id.goods_sort_wd_hot)
    RadioButton goodsSortWdHot;
    @BindView(R.id.goods_sort_wd_shaixuan)
    RadioButton goodsSortWdShaixuan;
    @BindView(R.id.goods_sort_wd_sort)
    LinearLayout goodsSortWdSort;
    @BindView(R.id.goods_sort_wd_fenge)
    View goodsSortWdFenge;
    @BindView(R.id.goods_sort_wd_Rv)
    MaxRecyclerView goodsSortWdRv;
    @BindView(R.id.goods_sort_wd_relativeLayout)
    RelativeLayout goodsSortWdRelativeLayout;
    @BindView(R.id.goods_sort_wd_drawer_fenlei_rv)
    RecyclerView drawerRv;
    @BindView(R.id.goods_sort_wd_DrawerLayout)
    DrawerLayout goodsSortWdDrawerLayout;
    @BindView(R.id.goods_sort_wd_drawer_Reset)
    TextView goodsSortWdDrawerReset;
    @BindView(R.id.goods_sort_wd_drawer_Sure)
    TextView goodsSortWdDrawerSure;
    @BindView(R.id.search_wd_SmartRefreshLayout)
    SmartRefreshLayout searchWdSmartRefreshLayout;
    @BindView(R.id.goods_sort_wd_LoadingLayout)
    LoadingLayout goodsSortWdLoadingLayout;
    private GoodsSortWdShowRvAdapter wdShowRvAdapter;
    private GoodsSortWdShowRvSxAdapter wdShowRvSxAdapter;
    private GoodsSortWdShowTimeRvAdapter wdShowTimeRvAdapter;
    private GoodsSortWdDrawerRvAdapter wdDrawerRvAdapter;
    private List<GoodsSortSearchAllWdBean.DataBean> wdBeanList = new ArrayList<>();
    private List<GoodsSortSearchAllWdBean.DataBean> wdSxBeanList = new ArrayList<>();
    private List<GoodsSortSearchAllWdBean.DataBean> wdTimeBeanList = new ArrayList<>();
    private ArrayList<GoodsSortWdDrwerBean> wdDrwerList = new ArrayList<>();
    private boolean timeFlag = false;
    private CarView carView;
    private String keyword;
    private TextView carTv;
    private int type;
    private GoodsSortJieJueShowRvAdapter goodsSortJieJueShowRvAdapter;
    private GoodsSortSearchAllWdApi searchAllApi;
    private int pageNo = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_sort_wd);
        ButterKnife.bind(this);
        goodsSortWdRv.setNestedScrollingEnabled(false);
        goodsSortWdDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        goodsSortWdZh.setSelected(true);

        Intent intent = getIntent();
        keyword = intent.getStringExtra("keyword");
        type = intent.getIntExtra("type", 0);

        initDrawerData();

        goodsSortBzEdit.setText(keyword);

        GridLayoutManager manager = new GridLayoutManager(this, 3);
        drawerRv.setLayoutManager(manager);
        wdDrawerRvAdapter = new GoodsSortWdDrawerRvAdapter(this);
        wdDrawerRvAdapter.setGoodsSortWdDrawerListener(this);
        drawerRv.setAdapter(wdDrawerRvAdapter);
        wdDrawerRvAdapter.setGoodsSortWdDrawerListener(this);

        //加载购物车商品数量
        loadData();
        initListener();
        initData(keyword);
        carView = new CarView(this);
        carTv = carView.findViewById(R.id.car_number);
        carView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GoodsSortWdActivity.this, ShoppingCarActivity.class);
                startActivity(intent);
            }
        });
        if (type==1){
            carView.hide();
        }else{
        carView.show();}
        goodsSortWdLoadingLayout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                HttpManager.getInstance().doHttpDeal(searchAllApi);
                goodsSortWdLoadingLayout.setStatus(LoadingLayout.LOADING);
            }

            @Override
            public void onReturn(View v) {
                onBackPressed();
            }
        });
        goodsSortWdLoadingLayout.setStatus(LoadingLayout.LOADING);
    }

    private void initListener() {
        goodsSortWdZh.setOnClickListener(this);
        goodsSortWdTime.setOnClickListener(this);
        goodsSortWdHot.setOnClickListener(this);
        goodsSortWdShaixuan.setOnClickListener(this);

        goodsSortWdBack.setOnClickListener(this);
        goodsSortWdFenlei.setOnClickListener(this);

        goodsSortWdDrawerReset.setOnClickListener(this);
        goodsSortWdDrawerSure.setOnClickListener(this);

        goodsSortBzEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //关闭软键盘
                    EditUtils.showORhideSoftKeyboard(GoodsSortWdActivity.this);
                    keyword = goodsSortBzEdit.getText().toString();
                    wdBeanList.clear();
                    goodsSortWdZh.setSelected(true);
                    goodsSortWdTime.setSelected(false);
                    goodsSortWdHot.setSelected(false);
                    goodsSortWdShaixuan.setSelected(false);
                    initData(keyword);
                    if (type != 1) {
                        wdShowRvAdapter.notifyDataSetChanged();
                    } else {
                        goodsSortJieJueShowRvAdapter.notifyDataSetChanged();
                    }
                    return true;
                }
                return false;
            }
        });
        searchWdSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNo = 1;
                wdBeanList.clear();
                initData(keyword);
                searchWdSmartRefreshLayout.finishRefresh(1000);
            }
        });
        searchWdSmartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                ++pageNo;
                initData(keyword);
                searchWdSmartRefreshLayout.finishLoadMore(1000);
            }
        });
    }

    private void setSortTimeDrawable(int p) {
        Drawable downTime = getResources().getDrawable(p);
        downTime.setBounds(0, 0, downTime.getMinimumWidth(), downTime.getMinimumHeight());
        goodsSortWdTime.setCompoundDrawables(null, null, downTime, null);
    }

    @Override
    protected void onResume() {
        loadData();
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.goods_sort_wd_zh:
                wdBeanList.clear();
                wdSxBeanList.clear();
                wdTimeBeanList.clear();
                goodsSortWdZh.setSelected(true);
                goodsSortWdTime.setSelected(false);
                goodsSortWdHot.setSelected(false);
                goodsSortWdShaixuan.setSelected(false);
                setSortTimeDrawable(R.mipmap.goods_sort_time_sort);
                initData(keyword);
                if (type != 1) {
                    wdShowRvAdapter.notifyDataSetChanged();
                } else {
                    goodsSortJieJueShowRvAdapter.notifyDataSetChanged();
                }
                break;
            case R.id.goods_sort_wd_time:
                wdBeanList.clear();
                wdSxBeanList.clear();
                wdTimeBeanList.clear();
                goodsSortWdZh.setSelected(false);
                goodsSortWdTime.setSelected(true);
                goodsSortWdHot.setSelected(false);
                goodsSortWdShaixuan.setSelected(false);
                if (timeFlag) {
                    setSortTimeDrawable(R.mipmap.down_time_sort2x);
                    initWdTimeData(0);
                } else {
                    setSortTimeDrawable(R.mipmap.up_time_sort2x);
                    initWdTimeData(1);
                }
                timeFlag = !timeFlag;
                if (type != 1) {
                    wdShowRvAdapter.notifyDataSetChanged();
                } else {
                    goodsSortJieJueShowRvAdapter.notifyDataSetChanged();
                }

                break;
            case R.id.goods_sort_wd_hot:
                wdBeanList.clear();
                wdSxBeanList.clear();
                wdTimeBeanList.clear();
                goodsSortWdZh.setSelected(false);
                goodsSortWdTime.setSelected(false);
                goodsSortWdHot.setSelected(true);
                goodsSortWdShaixuan.setSelected(false);
                setSortTimeDrawable(R.mipmap.goods_sort_time_sort);
                initData(keyword);
                if (type != 1) {
                    wdShowRvAdapter.notifyDataSetChanged();
                } else {
                    goodsSortJieJueShowRvAdapter.notifyDataSetChanged();
                }
                break;
            case R.id.goods_sort_wd_shaixuan:
                goodsSortWdZh.setSelected(false);
                goodsSortWdTime.setSelected(false);
                goodsSortWdHot.setSelected(false);
                goodsSortWdShaixuan.setSelected(true);
                setSortTimeDrawable(R.mipmap.goods_sort_time_sort);
                goodsSortWdDrawerLayout.openDrawer(GravityCompat.END);
                break;
            case R.id.goods_sort_wd_fenlei:
                Intent intent = new Intent(this, GoodsSortActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.goods_sort_wd_back:
                finish();
                break;
            case R.id.goods_sort_wd_drawer_Reset:
                setReset();
                break;
            case R.id.goods_sort_wd_drawer_Sure:
                initWdSureData();
                goodsSortWdDrawerLayout.closeDrawer(GravityCompat.END);
                break;
        }
    }

    private void initWdTimeData(int i) {
        GoodsSortSearchAllWdTimeApi searchAllSxApi = new GoodsSortSearchAllWdTimeApi(this, searchAllTimeListener, 1, 2055,i);
        searchAllSxApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(searchAllSxApi);
    }

    HttpOnNextListener<BaseResultEntity> searchAllTimeListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals("200")) {
                ToastTools.showToast(GoodsSortWdActivity.this, subjects.getMsg());
                Log.e("cjx", "文档数据：" + subjects.getMsg());
                return;
            }
            wdBeanList.clear();
            wdSxBeanList.clear();
            wdTimeBeanList.clear();
            ArrayList<GoodsSortSearchAllWdBean> wdBeans = (ArrayList<GoodsSortSearchAllWdBean>) subjects.getData();
            wdTimeBeanList.addAll(wdBeans.get(1).getData());
            goodsSortWdRv.setLayoutManager(new LinearLayoutManager(GoodsSortWdActivity.this));
            wdShowTimeRvAdapter = new GoodsSortWdShowTimeRvAdapter(GoodsSortWdActivity.this);
            goodsSortWdRv.setAdapter(wdShowTimeRvAdapter);
            wdShowTimeRvAdapter.setListener((GoodsSortWdShowTimeRvAdapter.GoodsWdTimeListener) GoodsSortWdActivity.this);
            wdShowTimeRvAdapter.setBeanList(wdTimeBeanList);
            wdShowTimeRvAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "wd数据" + e.getMessage());
        }
    };


    //获取文档数据监听
    private void initWdSureData() {
        int nowCatId = getWdNowCatId();
        GoodsSortSearchAllWdSxApi searchAllSxApi = new GoodsSortSearchAllWdSxApi(this, searchAllSxListener, 1, nowCatId, keyword);
        searchAllSxApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(searchAllSxApi);
    }

    HttpOnNextListener<BaseResultEntity> searchAllSxListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals("200")) {
                ToastTools.showToast(GoodsSortWdActivity.this, subjects.getMsg());
                Log.e("cjx", "文档数据：" + subjects.getMsg());
                return;
            }
            wdBeanList.clear();
            wdSxBeanList.clear();
            ArrayList<GoodsSortSearchAllWdBean> wdBeans = (ArrayList<GoodsSortSearchAllWdBean>) subjects.getData();
            wdSxBeanList.addAll(wdBeans.get(1).getData());
            goodsSortWdRv.setLayoutManager(new LinearLayoutManager(GoodsSortWdActivity.this));
            wdShowRvSxAdapter = new GoodsSortWdShowRvSxAdapter(GoodsSortWdActivity.this);
            goodsSortWdRv.setAdapter(wdShowRvSxAdapter);
            wdShowRvSxAdapter.setGoodsWdSxListener(GoodsSortWdActivity.this);
            wdShowRvSxAdapter.setBeanList(wdSxBeanList);
            wdShowRvSxAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "wd数据" + e.getMessage());
        }
    };


    private int getWdNowCatId() {
        int nowCatId = 0;
        for (int i = 0; i < wdDrwerList.size(); i++) {
            if (wdDrwerList.get(i).isSelected()) {
                nowCatId = wdDrwerList.get(i).getCatId();
            }
        }
        return nowCatId;
    }

    private void setReset() {
        for (int i = 0; i < wdDrwerList.size(); i++) {
            wdDrwerList.get(i).setSelected(false);
        }
        wdDrawerRvAdapter.notifyDataSetChanged();
    }

    //获取文档数据监听
    private void initData(String keyword) {

        if (type != 1) {
            searchAllApi = new GoodsSortSearchAllWdApi(this, searchAllListener, 1, 2055, keyword,pageNo,10);
            searchAllApi.setCache(false);
            HttpManager.getInstance().doHttpDeal(searchAllApi);
        } else {
            searchAllApi = new GoodsSortSearchAllWdApi(this, searchAllListener, 2, 460, "",pageNo,10);
            searchAllApi.setCache(false);
            HttpManager.getInstance().doHttpDeal(searchAllApi);
        }


    }

    HttpOnNextListener<BaseResultEntity> searchAllListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals("200")) {
                ToastTools.showToast(GoodsSortWdActivity.this, subjects.getMsg());
                Log.e("cjx", "文档数据：" + subjects.getMsg());
                return;
            }
            ArrayList<GoodsSortSearchAllWdBean> wdBeans = (ArrayList<GoodsSortSearchAllWdBean>) subjects.getData();
            goodsSortWdRv.setLayoutManager(new LinearLayoutManager(GoodsSortWdActivity.this));
            if (type != 1) {
                wdBeanList.addAll(wdBeans.get(1).getData());
                wdShowRvAdapter = new GoodsSortWdShowRvAdapter(GoodsSortWdActivity.this);
                goodsSortWdRv.setAdapter(wdShowRvAdapter);
                wdShowRvAdapter.setGoodsWdListener(GoodsSortWdActivity.this);
                wdShowRvAdapter.setBeanList(wdBeanList);
            } else {
                wdBeanList.addAll(wdBeans.get(1).getData());
                goodsSortJieJueShowRvAdapter = new GoodsSortJieJueShowRvAdapter(GoodsSortWdActivity.this);
                goodsSortWdRv.setAdapter(goodsSortJieJueShowRvAdapter);
                goodsSortJieJueShowRvAdapter.setBeanList(wdBeanList);
            }
            goodsSortWdLoadingLayout.setStatus(LoadingLayout.SUCCESS);
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "wd数据" + e.getMessage());
            if (e instanceof SocketTimeoutException || e instanceof ConnectException) {
                goodsSortWdLoadingLayout.setStatus(LoadingLayout.NO_NETWORK);
            } else {
                goodsSortWdLoadingLayout.setStatus(LoadingLayout.ERROR);
            }
        }
    };

    //获取文档属性数据监听
    private void initDrawerData() {
        GoodsSortWdDrawerApi wdDrawerApi = new GoodsSortWdDrawerApi(this, drawerBeanListener);
        wdDrawerApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(wdDrawerApi);
    }

    HttpOnNextListener<BaseResultEntity> drawerBeanListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals("200")) {
                ToastTools.showToast(GoodsSortWdActivity.this, subjects.getMsg());
                Log.e("cjx", "文档属性数据：" + subjects.getMsg());
                return;
            }
            wdDrwerList.addAll((ArrayList<GoodsSortWdDrwerBean>) subjects.getData());
            wdDrawerRvAdapter.setWdDrwerList(wdDrwerList);
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "wd属性数据" + e.getMessage());
        }
    };


    @Override
    protected int getContentViewId() {
        return 0;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void addListener() {

    }

    @Override
    public void setWdDrawerSelected(int index) {
        for (int i = 0; i < wdDrwerList.size(); i++) {
            if (i == index)
                wdDrwerList.get(i).setSelected(!wdDrwerList.get(i).isSelected());
            else
                wdDrwerList.get(i).setSelected(false);
        }
        wdDrawerRvAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        carView.destory();
    }

    //获取购物车列表数据
    private void loadData() {

        if (!getSharedPreferencesUtil().getLogintoken().isEmpty()) {
            final ShoppingCarListApi shoppingCarListApi = new ShoppingCarListApi(this, simpleOnNextListener, SharedPreferencesUtil.getLoginToken(), DeviceUtils.getIMEString(GoodsSortWdActivity.this));
            shoppingCarListApi.setCache(false);
            HttpManager.getInstance().doHttpDeal(shoppingCarListApi);
        }else {
            final ShoppingCarListApi shoppingCarListApi = new ShoppingCarListApi(this, simpleOnNextListener, "", DeviceUtils.getIMEString(GoodsSortWdActivity.this));
            shoppingCarListApi.setCache(false);
            HttpManager.getInstance().doHttpDeal(shoppingCarListApi);
        }
    }

    //获取购物车列表数据监听
    HttpOnNextListener simpleOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(GoodsSortWdActivity.this, subjects.getMsg());
                Log.e("cjx", subjects.getMsg());
                return;
            }
            ShoppingCarListBean bean = (ShoppingCarListBean) subjects.getData();
            int size = bean.getCartList().size();
            int num=0;
            for (int i = 0; i < size; i++) {
                int goodsNumber = bean.getCartList().get(i).getGoodsNumber();
                num+=goodsNumber;
            }
            Log.e("cjx", "初始数量：" + size + "");
            carTv.setText(num + "");
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "Bz购物车列表数据：" + e.getMessage());
        }
    };

    @Override
    public void addWdTimeCar(String skuStr) {
        SharedPreferencesUtil.getInstance().put("skuId", skuStr);
        String[] split = skuStr.split(":");
        String substring = split[0].substring(2, split[0].length() - 1);
        String substring2 = split[1].substring(0, split[1].length() - 1);
        getKuCun(Integer.parseInt(substring2), Integer.parseInt(substring));
    }

    //库存
    HttpOnNextListener httpOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            String skuId = SharedPreferencesUtil.getInstance().getAsString("skuId", "");
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                Toast.makeText(context, "商品库存不足", Toast.LENGTH_SHORT).show();
            } else {
                AddCar(skuId);
            }
        }
    };

    private void getKuCun(int num, int skuId) {
        ShoppingCarGoodsKuCunApi goodsKuCunApi = new ShoppingCarGoodsKuCunApi(GoodsSortWdActivity.this, httpOnNextListener, num, skuId);
        goodsKuCunApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(goodsKuCunApi);
    }

    private void AddCar(String skuStr) {
        Log.e("cjx", "skuStr:" + skuStr);
        if (!getSharedPreferencesUtil().getLogintoken().isEmpty()) {
            Map<String, String> map = new HashMap<>();
            map.put("skuStr", skuStr);
            map.put("orderType", 4 + "");
//            是否再次购买（0/1）
            map.put("buyAgain", 0 + "");
            map.put("token", getSharedPreferencesUtil().getLogintoken());
            map.put("deviceId", DeviceUtils.getIMEString(context));
            final AddCarApi addCarApi = new AddCarApi(GoodsSortWdActivity.this, simpleOnNextListenerAddCar);
            addCarApi.setCache(false);
            addCarApi.setMap(map);
            HttpManager.getInstance().doHttpDeal(addCarApi);
        }else {
            Map<String, String> map = new HashMap<>();
            map.put("skuStr", skuStr);
            map.put("orderType", 4 + "");
//            是否再次购买（0/1）
            map.put("buyAgain", 0 + "");
            map.put("token", "");
            map.put("deviceId", DeviceUtils.getIMEString(context));
            final AddCarApi addCarApi = new AddCarApi(GoodsSortWdActivity.this, simpleOnNextListenerAddCar);
            addCarApi.setCache(false);
            addCarApi.setMap(map);
            HttpManager.getInstance().doHttpDeal(addCarApi);
        }
    }

    //添加购物车
    HttpOnNextListener simpleOnNextListenerAddCar = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {

            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(context, subjects.getMsg());
                return;
            }
            double currentCarNumber = (double) subjects.getData();
            Log.e("cjx", "currentCarNumber:" + currentCarNumber + "");
            int i = (int) currentCarNumber;
            carTv.setText(i + "");
//            View toastView = LayoutInflater.from(GoodsSortBzActivity.this).inflate(R.layout.toast_addcar_success,null);
//            ToastTools.showToast(context,"加入购物车成功！");
            ToastTools.showToast("");
        }


        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
        }
    };

    @Override
    public void addWdSxCar(String skuStr) {
        SharedPreferencesUtil.getInstance().put("skuId", skuStr);
        String[] split = skuStr.split(":");
        String substring = split[0].substring(2, split[0].length() - 1);
        String substring2 = split[1].substring(0, split[1].length() - 1);
        getKuCun(Integer.parseInt(substring2), Integer.parseInt(substring));
    }

    @Override
    public void addWdCar(String skuStr) {
        SharedPreferencesUtil.getInstance().put("skuId", skuStr);
        String[] split = skuStr.split(":");
        String substring = split[0].substring(2, split[0].length() - 1);
        String substring2 = split[1].substring(0, split[1].length() - 1);
        getKuCun(Integer.parseInt(substring2), Integer.parseInt(substring));
    }
}
